package com.yunhe.option.service.base.impl;

import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.common.util.QueryUtil;
import com.yunhe.common.util.StringUtil;
import com.yunhe.option.domain.base.ProtectionEquipmentStatus;
import com.yunhe.option.repository.base.ProtectionEquipmentStatusRepository;
import com.yunhe.option.service.base.ProtectionEquipmentStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * 环保设备运行情况Service
 * @desc 环保设备运行情况服务类实现
 * @author anmeiyi
 */
@Service
public class ProtectionEquipmentStatusServiceImpl implements ProtectionEquipmentStatusService {
    @Autowired
    private ProtectionEquipmentStatusRepository protectionEquipmentStatusRepository;

    private void processBeforeSave(ProtectionEquipmentStatus protectionEquipmentStatus)
            throws ArgumentErrorException, UniqueConstraintsException {
        //判断环保设备运行情况代码、编号、标题是否为空
        if(protectionEquipmentStatus.getCode() == null
                || StringUtil.isEmpty(protectionEquipmentStatus.getName())
                || StringUtil.isEmpty(protectionEquipmentStatus.getTitle())) {
            throw new ArgumentErrorException("环保设备运行情况代码、编号、标题不能为空");
        }
        //判断代码是否已存在
        ProtectionEquipmentStatus categoryByCode = this.findByCode(protectionEquipmentStatus.getCode());
        if(categoryByCode != null) {
            if(protectionEquipmentStatus.getId() == null || categoryByCode.getId().longValue() != protectionEquipmentStatus.getId().longValue()) {
                throw new UniqueConstraintsException("该环保设备运行情况代码已存在");
            }
        }
        //判断编号是否已存在
        ProtectionEquipmentStatus categoryByName = this.findByName(protectionEquipmentStatus.getName());
        if(categoryByName != null) {
            if(protectionEquipmentStatus.getId() == null || categoryByName.getId().longValue() != protectionEquipmentStatus.getId().longValue()) {
                throw new UniqueConstraintsException("该环保设备运行情况编号已存在");
            }
        }
        //如果序号为空，则默认等于代码
        if(protectionEquipmentStatus.getSn() == null && protectionEquipmentStatus.getCode() != null) {
            protectionEquipmentStatus.setSn(protectionEquipmentStatus.getCode());
        }
    }

    /**
     * 新增环保设备运行情况
     * @param protectionEquipmentStatus 环保设备运行情况对象
     */
    @Transactional(rollbackFor = Exception.class, transactionManager = "transactionManager")
    public ProtectionEquipmentStatus createProtectionEquipmentStatus(ProtectionEquipmentStatus protectionEquipmentStatus)
            throws ArgumentErrorException, UniqueConstraintsException {
        protectionEquipmentStatus.setId(null);
        processBeforeSave(protectionEquipmentStatus);
        return protectionEquipmentStatusRepository.save(protectionEquipmentStatus);
    }

    /**
     * 删除环保设备运行情况
     * @param protectionEquipmentStatusId 环保设备运行情况Id
     */
    @Transactional(rollbackFor = Exception.class, transactionManager = "transactionManager")
    public void deleteProtectionEquipmentStatus(Long protectionEquipmentStatusId) {
        protectionEquipmentStatusRepository.deleteById(protectionEquipmentStatusId);
    }

    /**
     * 更新环保设备运行情况(全部字段）
     * @param protectionEquipmentStatus 环保设备运行情况对象
     */
    @Transactional(rollbackFor = Exception.class, transactionManager = "transactionManager")
    public ProtectionEquipmentStatus updateProtectionEquipmentStatus(ProtectionEquipmentStatus protectionEquipmentStatus)
            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
        boolean exists = protectionEquipmentStatus.getId() != null && protectionEquipmentStatusRepository.existsById(protectionEquipmentStatus.getId());
        if(!exists) {
            throw new ObjectNotFoundException("找不到ID为" + protectionEquipmentStatus.getId() + "的环保设备运行情况");
        }
        processBeforeSave(protectionEquipmentStatus);
        return protectionEquipmentStatusRepository.save(protectionEquipmentStatus);
    }

    /**
     * 更新环保设备运行情况（部分字段）
     * @param protectionEquipmentStatusId 环保设备运行情况对象
     * @param code
     * @param name
     * @param title
     * @param sn
     * @param activity
     */
    @Transactional(rollbackFor = Exception.class, transactionManager = "transactionManager")
    public ProtectionEquipmentStatus updateProtectionEquipmentStatus(Long protectionEquipmentStatusId, Long code, String name, String title, Long sn, Boolean activity)
            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
        ProtectionEquipmentStatus protectionEquipmentStatus = this.findByProtectionEquipmentStatusId(protectionEquipmentStatusId);
        if(protectionEquipmentStatus == null) {
            throw new ObjectNotFoundException("找不到ID为" + protectionEquipmentStatusId + "的环保设备运行情况");
        }
        Optional.ofNullable(code).ifPresent(protectionEquipmentStatus::setCode);
        Optional.ofNullable(name).ifPresent(protectionEquipmentStatus::setName);
        Optional.ofNullable(title).ifPresent(protectionEquipmentStatus::setTitle);
        Optional.ofNullable(sn).ifPresent(protectionEquipmentStatus::setSn);
        Optional.ofNullable(activity).ifPresent(protectionEquipmentStatus::setActivity);
        processBeforeSave(protectionEquipmentStatus);
        return protectionEquipmentStatusRepository.save(protectionEquipmentStatus);
    }

    /**
     * 获得全部环保设备运行情况对象
     */
    @Transactional(rollbackFor = Exception.class, transactionManager = "transactionManager")
    public Iterable<ProtectionEquipmentStatus> findIndustryCategories() {
        return protectionEquipmentStatusRepository.findAll(QueryUtil.defaultSort());
    }

    /**
     * 根据编号获得环保设备运行情况对象
     * @param name
     */
    private ProtectionEquipmentStatus findByName(String name) {
        return protectionEquipmentStatusRepository.findByName(name);
    }

    /**
     * 根据代码获得环保设备运行情况对象
     * @param code
     */
    private ProtectionEquipmentStatus findByCode(Long code) {
        return protectionEquipmentStatusRepository.findByCode(code);
    }

    /**
     * 根据ID获得环保设备运行情况对象
     * @param protectionEquipmentStatusId
     * @return
     */
    private ProtectionEquipmentStatus findByProtectionEquipmentStatusId(Long protectionEquipmentStatusId) {
        return protectionEquipmentStatusRepository.findById(protectionEquipmentStatusId).orElse(null);
    }
}
